//
//  UnLockViewController.swift
//  MyOfO
//
//  Created by Yuri on 2018/3/29.
//  Copyright © 2018年 Yuri. All rights reserved.
//

import UIKit
import SwiftyTimer
import AVFoundation
import SwiftySound
import AVOSCloud

class UnLockViewController: UIViewController {
    
    //车牌号
    var chePaiCode = 0
    //解锁码
    var passcode = 0
    
    //加个花括号，属性监视器
    var passArray : [String] = []
    
    @IBOutlet weak var numberLabel1: MyPreviewLabel!
    
    @IBOutlet weak var numberLabel2: MyPreviewLabel!
    
    @IBOutlet weak var numberLabel3: MyPreviewLabel!
    @IBOutlet weak var numberLabel4: MyPreviewLabel!
    @IBOutlet weak var torchBtn: UIButton!
    @IBOutlet weak var voiceBtn: UIButton!
    var isTorchOn = false
    var isVoiceOn = true
    
    @IBOutlet weak var countDownLabel: UILabel!
    var remainSecond = 121
    
    @IBAction func reportBtnTap(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func torchBtnTap(_ sender: UIButton) {
        isTorchOn = !isTorchOn
        
        if isTorchOn {
            torchBtn.setImage(#imageLiteral(resourceName: "btn_enableTorch"), for: .normal)
        } else {
            torchBtn.setImage(#imageLiteral(resourceName: "btn_unenableTorch"), for: .normal)
        }
        //开关后置闪光灯
        Utils.turnTorch()
    }
    
    @IBAction func voiceBtnTap(_ sender: UIButton) {
        isVoiceOn = !isVoiceOn
        
        if isVoiceOn {
            voiceBtn.setImage(#imageLiteral(resourceName: "voiceopen"), for: .normal)
        } else {
            voiceBtn.setImage(#imageLiteral(resourceName: "voiceclose"), for: .normal)
        }
        
        UserDefaults.standard.set(isVoiceOn, forKey: "isVoiceOn")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //每一秒执行一次
        Timer.every(1) { (timer:Timer) in
            self.remainSecond -= 1
            
            self.countDownLabel.text = self.remainSecond.description
            
            if self.remainSecond == 0 {
                timer.invalidate()
            }
        }
        
        isVoiceOn = UserDefaults.standard.bool(forKey: "isVoiceOn")
        if isVoiceOn {
            Sound.play(file: "上车前.m4a")
        }
        if isVoiceOn {
            voiceBtn.setImage(#imageLiteral(resourceName: "voiceopen"), for: .normal)
        } else {
            voiceBtn.setImage(#imageLiteral(resourceName: "voiceclose"), for: .normal)
        }
        
        numberLabel1.text = passArray[0]
        numberLabel2.text = passArray[1]
        numberLabel3.text = passArray[2]
        numberLabel4.text = passArray[3]
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
